-module(ebbs_util).
-define(MILLION, 1000000).
-compile(export_all).

binary_to_hexstring(B) ->
    byte_list_to_hexstring(binary_to_list(B)).

byte_list_to_hexstring(L) ->
    L1 = byte_list_to_hexstring(L, []),
    lists:reverse(L1).

byte_list_to_hexstring([], Acc) ->
    Acc;

byte_list_to_hexstring([Byte | Tail], Acc) ->
    High = Byte bsr 4,
    Low = Byte band 16#F,
    HighChar = four_bits_to_hexchar(High),
    LowChar = four_bits_to_hexchar(Low),
    byte_list_to_hexstring(Tail, [LowChar, HighChar | Acc]).

four_bits_to_hexchar(B) when B < 10 -> $0 + B;

four_bits_to_hexchar(B) -> $a + B - 10.

now() ->
    {MegaSecs, Secs, MicroSecs} = erlang:now(),
    (MegaSecs * ?MILLION + Secs) * ?MILLION + MicroSecs.

now_to_erlang_now(Now) ->
    MicroSecs = Now rem ?MILLION,
    I = Now div ?MILLION,
    Secs = I rem ?MILLION,
    MegaSecs = I div ?MILLION,
    {MegaSecs, Secs, MicroSecs}.
